package com.uenpay.im.invokeitems;

import android.text.TextUtils;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @function 创建群聊
 */
public class CreateGroupInvokeItem extends BaseHttpInvokeItem {
	public class CreateGroupResult extends HttpInvokeResult {
		public int status;
		public String msg;
		public String chatId;
		public String name;
		public int cust_max_count = 100;
		public int cust_count = 0;
	}

	public CreateGroupResult getOutput() {
		return (CreateGroupResult) GetResultObject();
	}

	public CreateGroupInvokeItem(String chatname, String ids, String amount) {
		String url = "";
		HashMap<String, String> mRequestParams = new HashMap<String, String>();
		if (TextUtils.isEmpty(amount)) {
			url = APIConfiguration.getBaseUrl() + "Chat/Chat/createChat?"
					+ APIConfiguration.getCustIdAndToken();
		} else {
			url = APIConfiguration.getBaseUrl()
					+ "Chat/SpecialChat/createFundsChat?"
					+ APIConfiguration.getCustIdAndToken();
			mRequestParams.put("amount", amount);
		}
		mRequestParams.put("chatname", chatname);
		mRequestParams.put("custids", ids);
		SetRequestParams(mRequestParams);
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		CreateGroupResult result = new CreateGroupResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		if (result.status != 0) {
			return result;
		}
		JSONObject jo = json.optJSONObject("data");
		if (jo != null) {
			result.chatId = jo.optString("chatid");
			result.cust_count = jo.optInt("custnum");
			result.cust_max_count = jo.optInt("maxcustnum");
			result.name = jo.optString("chatname");
		}

		return result;
	}
}
